<?php
namespace common\modules\user\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%user_auth_client}}".
 *
 * @property string $id
 * @property integer $user_id
 * @property string $provider
 * @property string $provider_user_id
 * @property string $provider_access_token
 * @property string $provider_refresh_token
 * @property string $provider_attributes
 * @property integer $create_time
 * @property integer $update_time
 *
 * @author Xuegui Yuan <yuanxuegui@163.com>
 */
class UserAuthClient extends ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user_auth_client}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'updatedAtAttribute' => false
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'user_id' => Yii::t('common', 'User ID'),
            'provider' => Yii::t('common', 'Provider'),
            'provider_user_id' => Yii::t('common', 'Provider User ID'),
            'provider_access_token' => Yii::t('common', 'Provider Access Token'),
            'provider_refresh_token' => Yii::t('common', 'Provider Refresh Token'),
            'provider_attributes' => Yii::t('common', 'Provider Attributes'),
            'create_time' => Yii::t('common', 'Create Time'),
            'update_time' => Yii::t('common', 'Update Time')
        ];
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), [
            'id' => 'user_id'
        ]);
    }

    /**
     * Set user id
     *
     * @param int $userId            
     * @return static
     */
    public function setUser($userId)
    {
        $this->user_id = $userId;
        return $this;
    }

    /**
     * Set provider attributes
     *
     * @param array $attributes            
     * @return static
     */
    public function setProviderAttributes($attributes)
    {
        $this->provider_attributes = json_encode($attributes);
        return $this;
    }

    /**
     * Get provider attributes
     *
     * @return array
     */
    public function getProviderAttributes()
    {
        return json_decode($this->provider_attributes, true);
    }
}
